package com.ourteam.education.api;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.education.dao.CourseTypeDAOQueryBean;
import com.ourteam.education.domain.CourseTypeBean;
import com.ourteam.education.service.CourseManageServiceFactory;
import com.ourteam.education.service.ICourseManageService;
import com.ourteam.frame.controller.AbstractCRUDServiceController;

@Controller
@CrossOrigin
@RequestMapping("/api/EDUCATION/courseType")
public class CourseTypeMagaeAPI
		extends AbstractCRUDServiceController<CourseTypeDAOQueryBean, CourseTypeSearchBean, CourseTypeBean> {

	private static final ICourseManageService COURSE_MANAGE_SERVICE = CourseManageServiceFactory
			.getCourseManageService();

	@Override
	protected CourseTypeDAOQueryBean newQueryBean() {
		return new CourseTypeDAOQueryBean();
	}

	@Override
	protected int queryCount(CourseTypeDAOQueryBean queryBean) throws Exception {
		return COURSE_MANAGE_SERVICE.queryCourseTypeCount(queryBean);
	}

	@Override
	protected CourseTypeBean[] queryData(CourseTypeDAOQueryBean queryBean) throws Exception {
		return COURSE_MANAGE_SERVICE.queryCourseTypes(queryBean);
	}

	@Override
	protected void deleteData(long[] ids) throws Exception {

		COURSE_MANAGE_SERVICE.deleteCourseTypes(ids);
	}

	@Override
	protected CourseTypeBean getDataById(long id) throws Exception {
		return COURSE_MANAGE_SERVICE.getCourseTypeById(id);
	}

	@Override
	protected void saveData(CourseTypeBean dataBean) throws Exception {

		if (dataBean.getId() != 0) {
			COURSE_MANAGE_SERVICE.modifyCourseType(dataBean);
		} else {
			COURSE_MANAGE_SERVICE.addNewCourseType(dataBean);
		}

	}

	@RequestMapping("/list")
	@ResponseBody
	public CourseTypeBean[] query(final @RequestParam(required=false) String typeName) throws Exception {

		CourseTypeDAOQueryBean searchBean = new CourseTypeDAOQueryBean();

		CourseTypeDAOQueryBean.Criteria criteria = searchBean.createCriteria();
		
		criteria.andStatusEqualTo("available");

		if (StringUtils.isNoneBlank(typeName)) {
			criteria.andCourseTypeNameLike(typeName);
		}

		return COURSE_MANAGE_SERVICE.queryCourseTypes(searchBean);
	}

}
